package edu.pku.hawk.service.launcher;

import java.util.Vector;

import org.uddi4j.client.UDDIProxy;
import org.uddi4j.datatype.OverviewDoc;
import org.uddi4j.datatype.binding.AccessPoint;
import org.uddi4j.datatype.binding.BindingTemplate;
import org.uddi4j.datatype.binding.TModelInstanceDetails;
import org.uddi4j.datatype.binding.TModelInstanceInfo;
import org.uddi4j.datatype.business.BusinessEntity;
import org.uddi4j.datatype.service.BusinessService;
import org.uddi4j.datatype.service.BusinessServices;
import org.uddi4j.datatype.tmodel.TModel;
import org.uddi4j.response.AuthToken;
import org.uddi4j.response.BindingDetail;
import org.uddi4j.response.BusinessDetail;
import org.uddi4j.response.TModelDetail;

public class SaveWebserviceURI {
	
	public static boolean savewebserviceURI(String webservicename,String webserviceuri, String publishurl){
		
		try{
			UDDIProxy proxy = new UDDIProxy();
			proxy.setInquiryURL(publishurl+"/juddi/inquiry");
			proxy.setPublishURL(publishurl+"/juddi/publish");
			System.out.println("\nGet authtoken");
			AuthToken token = proxy.get_authToken("juddi","juddi");
			System.out.println("Returned authToken:" + token.getAuthInfoString());
			Vector entities = new Vector();
			/*Business key is empty,because it's generated by the server, the name is ChrisBusinessEntity*/
			BusinessEntity be = new BusinessEntity("", "my"+webservicename);
			be.setDefaultDescriptionString("This is a test service");
			entities.addElement(be);
			BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),entities);
			Vector businessEntities = bd.getBusinessEntityVector();
			BusinessEntity returnedBusinessEntity = (BusinessEntity)(businessEntities.elementAt(0));
			System.out.println("Returned businessKey:" + returnedBusinessEntity.getBusinessKey());
			
			
			System.out.println("Business Name: "+returnedBusinessEntity.getDefaultNameString());
			
			
		}catch(Exception e){
			System.err.println("Business Failed");
		}

		
		
		try{
			UDDIProxy proxy = new UDDIProxy();
			proxy.setInquiryURL(publishurl+"/juddi/inquiry");
			proxy.setPublishURL(publishurl+"/juddi/publish");
			System.out.println("\nGet authtoken");
			AuthToken token = proxy.get_authToken("juddi","juddi");
			System.out.println("Returned authToken:" + token.getAuthInfoString());
			Vector entities = new Vector();
			BusinessEntity be =
				new BusinessEntity("","my"+webservicename);
			BusinessService businessService = new BusinessService("");
			businessService.setDefaultNameString("my"+webservicename,null);
			businessService.setBusinessKey ("");
			Vector services = new Vector();
			services.addElement(businessService);
			BusinessServices businessServices = new BusinessServices ();
			businessServices.setBusinessServiceVector (services);
			// Adding the BusinessServices to the BusinessEntity
			be.setBusinessServices (businessServices);
			entities.addElement(be);
			// Save business
			BusinessDetail bd = proxy.save_business(token.getAuthInfoString(),entities);
			// Process returned BusinessDetail object to get the
			// Business Key and Service Key.
			Vector businessEntities = bd.getBusinessEntityVector();
			BusinessEntity returnedBusinessEntity =(BusinessEntity)(businessEntities.elementAt(0));
			String businessKey = returnedBusinessEntity.getBusinessKey();
			Vector businessServicesReturned =
				returnedBusinessEntity.getBusinessServices().getBusinessServiceVector();
			BusinessService businessServiceReturned =
				(BusinessService)(businessServicesReturned.elementAt(0));
			String serviceKey = businessServiceReturned.getServiceKey();
			Vector tModels = new Vector();
			TModel tModel = new TModel("","my"+webservicename);
			OverviewDoc overview = new OverviewDoc();
			overview.setOverviewURL(webserviceuri);
			tModel.setOverviewDoc(overview);
			tModels.add(tModel);
			TModelDetail tModelDetail = proxy.save_tModel(token.getAuthInfoString(), tModels);
			// Creating TModelInstanceDetails
			Vector tModelVector = tModelDetail.getTModelVector();
			String tModelKey = ((TModel)(tModelVector.elementAt(0))).getTModelKey();
			Vector tModelInstanceInfoVector = new Vector();
			TModelInstanceInfo tModelInstanceInfo = new TModelInstanceInfo(tModelKey);
			tModelInstanceInfoVector.add(tModelInstanceInfo);
			TModelInstanceDetails tModelInstanceDetails = new TModelInstanceDetails();
			tModelInstanceDetails.setTModelInstanceInfoVector(tModelInstanceInfoVector);

			Vector bindingTemplatesVector = new Vector();

			// Create a new binding templates using required elements constructor
			// BindingKey must be "" to save a new binding
			BindingTemplate bindingTemplate = new BindingTemplate("",tModelInstanceDetails,new AccessPoint(webserviceuri,"http"));
			bindingTemplate.setServiceKey(serviceKey);
			bindingTemplatesVector.addElement(bindingTemplate);
			BindingDetail bindingDetail = proxy.save_binding(token.getAuthInfoString(),bindingTemplatesVector);

			// Process returned BindingDetail object
			Vector bindingTemplateVector = bindingDetail.getBindingTemplateVector();
			BindingTemplate bindingTemplateReturned = (BindingTemplate)(bindingTemplateVector.elementAt(0));
			System.out.println("Returned BindingKey: " +bindingTemplateReturned.getBindingKey());
			
			
		}catch(Exception e){
			System.out.println("Service Binding Failed.");
		}
		
		try{
			UDDIProxy proxy = new UDDIProxy();
			proxy.setInquiryURL(publishurl+"/juddi/inquiry");
			proxy.setPublishURL(publishurl+"/juddi/publish");
			System.out.println("\nGet authtoken");
			AuthToken token = proxy.get_authToken("juddi","juddi");
			System.out.println("Returned authToken:" + token.getAuthInfoString());
			System.out.println("\nSaving tModel......");
			Vector tModels = new Vector();
			/*Also tModel key is generated by the server, so we only need to give the name attribute*/
			TModel tModel = new TModel("", "my"+webservicename);
			OverviewDoc overview = new OverviewDoc();
			overview.setOverviewURL(webserviceuri);
//			overview.getOverviewURLString();
			tModel.setOverviewDoc(overview);
			tModels.add(tModel);
			TModelDetail tModelDetail = proxy.save_tModel(token.getAuthInfoString(), tModels);
			Vector tModelVector = tModelDetail.getTModelVector();
			TModel tModelReturned = (TModel)(tModelVector.elementAt(0));
			System.out.println("TModel Saved: " + tModelReturned.getNameString());
			System.out.println("TModel Key  : " + tModelReturned.getTModelKey());
			
			
			OverviewDoc overview2 = tModelReturned.getOverviewDoc();
			System.out.println(overview2.getOverviewURLString());
			
		}catch(Exception e){
			System.err.println("Failed");
		}
		
		
		return true;
		
	}
	
	public static void main(String args[]){
		
		String keyword = "Rabbit";
		String webserviceURI = "http://172.16.0.97:8080/axis/HelloClient.jws?wsdl";
		String publishURL = "http://172.16.0.97:8080";
		
		if(savewebserviceURI(keyword,webserviceURI,publishURL))
			System.out.println("\nSave Webservice URI succeed.");
		else
			System.out.println("\nSave Webservice URI failed.");
		
	}

}
